package com.sgb.kjwl.view.ui.activity.self;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddressInvoiceBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.link.entity.InvoiceManagementEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.adapter.self.InvoiceManagementAdapter;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.InvoiceManagementModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 收款账号
 */
@Route(path = AppRouteConstant.INVOICE_MANAGEMENT)
public class InvoiceManagementActivity extends AppActivity {

    @Inject
    InvoiceManagementModel viewModel;
    private ActivityAddressInvoiceBinding binding;
    private InvoiceManagementAdapter adapter;
    private List<InvoiceManagementEntity> arrayList;
    private String companyNo;
    //使用此字段判断从哪里跳转来的  1.集采-供方发起立即参与页面
    private int type;
    @Autowired(name = "companyNo")
    public  String companyNos;
    @Autowired(name = "Type")
    public   int Types;
    @Autowired(name = "isARouter")
    public   boolean isARouter;
    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent)
                .selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        String tag = getIntent().getStringExtra("TAG");
        if (isARouter){
            companyNo = companyNos;
            type = Types;
        }else {
            companyNo = getIntent().getStringExtra("companyNo");
            type = getIntent().getIntExtra("type", 0);
        }
        binding = DataBindingUtil.setContentView(InvoiceManagementActivity.this, R.layout.activity_address_invoice);
        binding.include.titleView.setText("开票信息");
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setText("新增开票");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
//        binding.tvAddAddress.setOnClickListener(this);

        adapter = new InvoiceManagementAdapter(this, null);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_line)));
        binding.recyclerView.setAdapter(adapter);
        adapter.setonEditClick(new InvoiceManagementAdapter.onEditClickListener() {
            @Override
            public void onEditClick(InvoiceManagementEntity data) {
                InvoiceAddActivity.start(InvoiceManagementActivity.this, data.getId(), companyNo);
            }

            @Override
            public void onDelClick(String id) {
                //viewModel.delIdAddress(id);
                //SwipeMenu.closeMenu();
                showDeleteDialog(id);
            }

            @Override
            public void isCheck(boolean isCheck, int position) {
                arrayList.get(position).setIsDefault(1);
                viewModel.addBill(arrayList.get(position));
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<InvoiceManagementEntity>() {
            @Override
            public void onItemClick(InvoiceManagementEntity invoiceManagementEntity, int position) {
                if (type == 1) {
                    //集采跳转过来选择开票信息
                    LeaseEvent event = new LeaseEvent();
                    event.setMessageTag(EventConstant.EVENT_JICAI_FACTORY_LIJICANYU_KAIPIAOXINXI);
                    event.setMessageData(invoiceManagementEntity);
                    EventBus.getDefault().postSticky(event);
                    finish();
                }
                if (type == 2) {
                    //商品跳转过来选择开票信息
                    LeaseEvent event = new LeaseEvent();
                    event.setMessageTag(EventConstant.EVENT_GOODS_FACTORY_LIJICANYU_KAIPIAOXINXI);
                    event.setMessageData(invoiceManagementEntity);
                    EventBus.getDefault().postSticky(event);
                    finish();
                } else {
                    if ("CHECK_ORDER".equals(tag)) {
                        EventBus.getDefault().postSticky(invoiceManagementEntity);
                        finish();
                    }
                }
            }
        });

        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getBillList(companyNo);
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_JICAI_FACTORY_LIJICANYU_KAIPIAOXINXI) {
            finish();
        }
    }

    @Override
    protected void init() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getBillList(companyNo);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                InvoiceAddActivity.start(this, null, companyNo);
                break;
        }
    }

    public void showInfo(List<InvoiceManagementEntity> arrayList) {
        this.arrayList = arrayList;
        binding.recyclerView.refreshComplete();
        binding.nullView.setVisibility(arrayList.size() == 0 ? View.VISIBLE : View.GONE);
        adapter.setDatas(arrayList);
    }

    /**
     * 展示删除的dialog
     *
     * @param id
     */
    private void showDeleteDialog(final String id) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("确定要删除当前开票信息？");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求删除接口
                viewModel.deleteBill(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
